package com.pf.commom.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2021/8/27
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        String userName = metaObject.getValue("createdBy") != null ? (String) metaObject.getValue("createdBy") : "system";
        this.setFieldValByName("isDeleted", 0, metaObject);
        this.setFieldValByName("version", 0, metaObject);
        this.setFieldValByName("createdBy", userName, metaObject);
        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("updatedBy", userName, metaObject);
        this.setFieldValByName("updatedTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userName = metaObject.getValue("updatedBy") != null ? (String) metaObject.getValue("updatedBy") : "system";
        this.setFieldValByName("updatedBy", userName, metaObject);
        this.setFieldValByName("updatedTime", new Date(), metaObject);
    }
}
